package com.wcpdoc.exam.core.dao.impl;

import com.wcpdoc.core.dao.impl.RBaseDaoImpl;
import com.wcpdoc.core.entity.PageIn;
import com.wcpdoc.core.entity.PageOut;
import com.wcpdoc.core.entity.PageResult;
import com.wcpdoc.core.entity.PageResultEx;
import com.wcpdoc.core.util.DateUtil;
import com.wcpdoc.core.util.HibernateUtil;
import com.wcpdoc.core.util.SqlUtil;
import com.wcpdoc.core.util.SqlUtil.Order;
import com.wcpdoc.exam.core.dao.MyExamMockDao;
import com.wcpdoc.exam.core.entity.MyExamMock;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 我的模拟考试数据访问层实现
 * 
 */
@Repository
public class MyExamMockDaoImpl extends RBaseDaoImpl<MyExamMock> implements MyExamMockDao {

	@Override
	public PageOut getListpage(PageIn pageIn) {
		String sql = "SELECT MY_EXAM.NAME, MY_EXAM.ID, MY_EXAM.PAPER_NAME, MY_EXAM.UPDATE_TIME,"// 考试相关字段
				+ "MY_EXAM.SCORE_STATE, "// 考试相关字段（用于控制是否显示成绩和排名，控制层用）
				+ "MY_EXAM.PASS_TIME,MY_EXAM.PASS_SCORE, "// 考试相关字段
				+ "USER.ID AS USER_ID, USER.NAME AS USER_NAME, "// 考试用户字段
				+ "MY_EXAM.ANSWER_START_TIME, MY_EXAM.ANSWER_END_TIME, MY_EXAM.TOTAL_SCORE, MY_EXAM.EXAM_TOTAL_SCORE, MY_EXAM.STATE, "// 我的考试字段
				+ "MY_EXAM.MARK_STATE, MY_EXAM.ANSWER_STATE, MY_EXAM.NO "// 我的考试字段
				+ "FROM EXM_MY_EXAM_MOCK MY_EXAM "
				+ "LEFT JOIN SYS_USER USER ON MY_EXAM.USER_ID = USER.ID ";

		SqlUtil sqlUtil = new SqlUtil(sql);
		sqlUtil
//				.addWhere(ValidateUtil.isValid(pageIn.get("examName")), "MY_EXAM.NAME LIKE :NAME", "%" + pageIn.get("examName") + "%")
				.addWhere(pageIn.get("curUserId", Integer.class) != null, "MY_EXAM.USER_ID = :USER_ID", pageIn.get("curUserId", Integer.class))
//				.addWhere(ValidateUtil.isValid(pageIn.get("startTime")) && ValidateUtil.isValid(pageIn.get("endTime")),
//						"(( :START_TIME1 <= EXAM.START_TIME AND EXAM.START_TIME <= :END_TIME1) "
//						+ "	OR ( :START_TIME2 <= EXAM.END_TIME AND EXAM.END_TIME <= :END_TIME2) "
//						+ "	OR ( :START_TIME3 >= EXAM.START_TIME AND EXAM.END_TIME >= :END_TIME3))",
//						pageIn.get("startTime"), pageIn.get("endTime"),
//						pageIn.get("startTime"), pageIn.get("endTime"),
//						pageIn.get("startTime"), pageIn.get("endTime")
//						)
//				.addWhere("true".equals(pageIn.get("todo")), "((MY_EXAM.STATE = 1 AND MY_EXAM.MARK_STATE != 3) OR MY_EXAM.STATE = 2)")// 查找我的未完成的考试列表
//				.addWhere("MY_EXAM.STATE = 1")// 已发布（不含冻结）
				.addOrder("MY_EXAM.UPDATE_TIME", Order.DESC);// 按考试开始时间倒序排列
		PageOut pageOut = getListpage(sqlUtil, pageIn);
		HibernateUtil.formatDate(pageOut.getList(), 
//				"examStartTime", DateUtil.FORMAT_DATE_TIME,
//				"examEndTime", DateUtil.FORMAT_DATE_TIME,
//				"examMarkEndTime", DateUtil.FORMAT_DATE_TIME,
//				"examMarkStartTime", DateUtil.FORMAT_DATE_TIME,
				"answerStartTime", DateUtil.FORMAT_DATE_TIME, 
				"answerEndTime", DateUtil.FORMAT_DATE_TIME);
		return pageOut;
	}

	@Override
	public List<MyExamMock> getList(Integer examId) {
		String sql = "SELECT * FROM EXM_MY_EXAM_MOCK WHERE ID = :EXAM_ID";
		return getList(sql, new Object[] { examId }, MyExamMock.class);
	}
	
	@Override
	public List<MyExamMock> getListForUser(Integer userId) {
		String sql = "SELECT * FROM EXM_MY_EXAM_MOCK WHERE USER_ID = :USER_ID";
		return getList(sql, new Object[] { userId }, MyExamMock.class);
	}

	@Override
	public MyExamMock getMyExamMock(Integer examId, Integer userId) {
		String sql = "SELECT * FROM EXM_MY_EXAM_MOCK WHERE ID = :EXAM_ID AND USER_ID = :USER_ID";
		return getEntity(sql, new Object[] { examId, userId });
	}

//	@Override
//	public List<MyExamMock> getExamList(Integer userId) {
//		String sql = "SELECT EXAM.* "
//				+ "FROM EXM_EXAM_MOCK EXAM "
//				+ "WHERE EXAM.STATE != 0 "
//				+ "	AND EXISTS (SELECT 1 FROM EXM_MY_EXAM Z WHERE Z.USER_ID = :USER_ID AND ID = Z.ID) ";
//		return getList(sql, new Object[] { userId }, Exam.class);
//	}

	@Override
	public void clear(Integer examId) {
		String sql = "DELETE FROM EXM_MY_EXAM_MOCK WHERE ID = :EXAM_ID";
		update(sql, new Object[] { examId });
	}

	@Override
	public MyExamMock examUnStart(Integer userId) {
		String sql = "SELECT * FROM EXM_MY_EXAM_MOCK WHERE USER_ID = :USER_ID AND STATE != 3 LIMIT 1";
		MyExamMock myExamMock = getEntity(sql, new Object[] {userId});
		return myExamMock;
	}

	/**
	 * 是否存在正在进行的模拟考试
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public PageResult hasJxMockExam(Integer userId) {
		String sql = "SELECT COUNT(1) FROM EXM_MY_EXAM_MOCK WHERE USER_ID = :USER_ID AND STATE != 3 ";
		Integer nCount= getCount(sql, new Object[] { userId });
		return nCount>0? PageResultEx.ok().data(true):PageResultEx.ok().data(false);
	}

	/**
	 * 超时未提交的模拟考试
	 *
	 * @return
	 */
	@Override
	public List<MyExamMock> getAllOverMyExam() {
		String sql = "select a.* from exm_my_exam_mock a where a.STATE!=3 and  timestampdiff(MINUTE,a.ANSWER_START_TIME,NOW())>a.PASS_TIME ";
		return getList(sql,new Object[]{});
	}

	/**
	 * 获取用户的试题库试题
	 *
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getMyQuestionFromRule(Integer userId) {
		String sql = "select "
				+ "a.ID,"
				+ "a.TYPE "
				+ "from exm_question a "
				+ "inner join exm_user_question_type b on a.QUESTION_TYPE_ID=b.QUESTION_TYPE_ID "
				+ "where a.STATE=1 AND b.USER_ID= :USER_ID ;";
		List<Map<String, Object>> mapList = getMapList(sql, new Object[] { userId });
//		List<ExamMockInfo> list = new ArrayList<>(mapList.size());
//		for (Map<String, Object> map : mapList) {
//			mapList.get(i).get("")
//			list.add((Integer)map.get("questionId"));
//		}
		return mapList;
//		return getList(sql,new Object[]{userId});
	}

}